package com.huidz.calendar.daysfragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.huidz.calendar.R;
import com.huidz.calendar.activityadapter.JieRiAdapter;
import com.huidz.calendar.model.CalendarModel;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.Url;

import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class JieRiFragment extends Fragment {
    //用于获取视图
    private View root;
    private ListView jr_list;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.jierilistview, container, false);//初始加载
        if (Apiutils.isNetworkConnecteds)
            InitView(root);
        return root;
    }

    private ArrayList<CalendarModel.data> arrayList=new ArrayList<>();
    private JieRiAdapter jieRiAdapter;
    /**
     * 初始加载视图
     */
    public void InitView(View view) {
        jr_list = view.findViewById(R.id.jr_list);
        jieRiAdapter = new JieRiAdapter(arrayList,
                getActivity());
        jr_list.setAdapter(jieRiAdapter);
        okHttp_postFromParameters();
    }
    public void okHttp_postFromParameters() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = new FormBody.Builder().add("year", 2020+"")
                            .build();
                    Request request = new Request.Builder().url(Url.JIARI).post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    gethot.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public CalendarModel userm;
    public Handler gethot = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        userm = gson.fromJson(msg.obj.toString(),
                                CalendarModel.class);
                        arrayList.addAll(userm.getData());
                        jieRiAdapter.notifyDataSetChanged();
                    } else
                        Apiutils.SetToast(getActivity(), msgs + "");
                }
            } catch (
                    Exception e) {
                e.printStackTrace();
            }
        }
    };

}